/****************************************************************************
 *
 *  LinuxChatty
 *  Copyright (C) 2010, David Hsu.
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 ***************************************************************************/
#include "MainFrame.h"
#include "ui/UIManager.h"
#include <ncurses.h>

MainFrame::MainFrame(UIManager * m):
    manager(m)
{
    // Do nothing for now
}

MainFrame::~MainFrame()
{
    // No clean up for now
}

/**
 * Core loop of the program.  Constantly loops through key presses and passes
 * them to the UIManager.
 * @param threadDebug True if we're only looking at a specific thread.
 */
void MainFrame::Run( bool threadDebug)
{
	int c;
	bool shouldQuit = false;
	    
	// Add the initial UI to the manager for display
	manager->Clear();
	if ( threadDebug )
	{
    	manager->PushUI( "comments" );
	}
	else
	{
	    manager->PushUI( "mainmenu" );
	}
	manager->Refresh();
	
	while( !shouldQuit )
	{
		c = getch();
		
		// Check global commands (mostly for quitting or help)
		// DEBUG: For now, ESC will always kill the program immediately
		if (c == 27) // ESC key
		{
			shouldQuit = true;
		}
		
		// Pass it to the active screen
		manager->HandleInput(c);
		
		// If there's no screens left in the manager, end this
		if (manager->IsEmpty())
			break;
		
		manager->Refresh();
	}
}
